//app.js
App({
  globalData: {
    userInfo: null,
    URL: 'https://hejianwei.snslearn.com',
    URLBOOL: '&lgc=1'
  },
  login: function (obj = this) {
    var _this = this;
    wx.login({
      success: res => {
        wx.request({
          url: _this.globalData.URL + '/interface/WeixinApp/login?lgc=1&channel_no=' + wx.getStorageSync('channel_no'),
          data: {
            'code': res.code,
            'app_name': 'xiaochengxu_album',
            'openid': wx.getStorageSync('openid'),
          },
          method: 'GET',
          success: function (res) {
            if (res.data.error_code == 0) {
              wx.setStorageSync('token', res.data.data.token);
              wx.setStorageSync('openid', res.data.data.openid);
              wx.setStorageSync('bindAcountBool', res.data.data.bind_acount_bool);
              //console.log(wx.getStorageSync('token'));
              // obj.index();
              obj.loadCourseData();
            } else {
              console.log(res.data.error_msg);
            }
          }
        })
        //console.log(res);
      }
    })
    /*  wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
              success: res => {
                // 可以将 res 发送给后台解码出 unionId
                this.globalData.userInfo = res.userInfo
                wx.login({
                  success: res => {
                    wx.request({
                      url: _this.globalData.URL + '/interface/WeixinApp/login?lgc=1&channel_no=' + wx.getStorageSync('channel_no'),
                      data: {
                        'code': res.code,
                        'user_info': this.globalData.userInfo,
                        'app_name': 'xiaochengxu_album',
                        'openid': wx.getStorageSync('openid'),
                      },
                      
                      method: 'GET',
                      success: function (res) {
                        if (res.data.error_code == 0) {
                          wx.setStorageSync('token', res.data.data.token);
                          wx.setStorageSync('openid', res.data.data.openid);
                          wx.setStorageSync('bindAcountBool', res.data.data.bind_acount_bool);
                          wx.setStorageSync('userinfo', res.data.data.userinfo);
                          //console.log(wx.getStorageSync('token'));
                          obj.index();
                        } else {
                          //console.log(res.data.error_msg);
                        }
                      }
                    })
                    //console.log(res);
                  }
                })
                if (this.userInfoReadyCallback) {
                  this.userInfoReadyCallback(res)
                }
              }
            })
          }
        }
      })*/
  },
  //授权之后更新用户信息
  setUserInfoAndUpdateInfo: function (user_info) {
    this.globalData.userInfo = user_info;
    wx.request({
      url: this.globalData.URL + '/interface/WeixinApp/updateWeixinAppInfo?user_info=' + JSON.stringify(user_info) + this.globalData.URLBOOL,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
      }
    })
  },
  setGetUserInfo: function (obj = this) {
    var _this = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              _this.login(obj);
            },
            fail() {
              console.log('authSetting1');
            },
            complete() {
              console.log('authSetting2');
            }
          })
        } else {
          _this.login(obj);
        }
      }
    })
  },
  setUrlBool: function () {
    if (!wx.getStorageSync('token')) {
      this.globalData.URLBOOL = '&lgc=1';
    } else {
      this.globalData.URLBOOL = '&token=' + wx.getStorageSync('token');
    }
  },
  setAuth: function (obj = this) {
    wx.openSetting({
      success: (res) => {
        if (res.authSetting["scope.userInfo"] == true) {
          this.login(obj);
        } else {
        }
      }
    })
  },
  //判断是否有token
  judgeToken: function (nowThis, callback) {
    var Token = wx.getStorageSync('token')
    if (Token) {
      if (typeof callback == "function") {
        callback();
      }
      function doNext(res) {
        console.log(res)
      }
    } else {
      this.setAuth(nowThis);
    }
  },
  creatOrder: function (pay_type, id, price, name, nowThis) {
    var _this = this;
    wx.request({
      url: _this.globalData.URL + '/interface/Pay/weixinJsApiOrder&token=' + wx.getStorageSync('token'),
      data: {
        'type': pay_type,
        'id': id,
        'price': price,
        'name': name,
        'openid': wx.getStorageSync('openid'),
        'app_name': 'xiaochengxu_album',
        'channel_no': wx.getStorageSync('channel_no')
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res, callback) {
        // console.log("success中"+res)
        if (res.data.error_code == 0) {
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': 'MD5',
            'paySign': res.data.data.paySign,

            'success': function (res) {
              // console.log(res);
              nowThis.index();
              nowThis.setData({
                modelHide: true,
                modelHide2: true,
                show_play_btn: true,
                showVideo: false,
                accountLogin: true,
                buyAlbumModal: true,
              })
            },
            'fail': function (res) {
              //console.log(res);
            },
            'complete': function (res) {
              // console.log(res);
            }
          })


        } else {
          //console.log(res);
        }
      }
    })
  }
})